package com.hxzy.shop.commodity.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.shop.commodity.dto.req.EditPrescriptionReviewSettingsReq;
import com.hxzy.shop.commodity.mapper.PrescriptionReviewSettingsMapper;
import com.hxzy.shop.commodity.service.PrescriptionReviewSettingsService;
import com.hxzy.shop.constant.AuthConstant;
import com.hxzy.shop.entity.PrescriptionReviewSettings;
import com.hxzy.shop.utils.WebUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (PrescriptionReviewSettings)表服务实现类
 *
 * @author makejava
 * @since 2022-09-21 11:23:42
 */
@Service("prescriptionReviewSettingsService")
public class PrescriptionReviewSettingsServiceImpl extends ServiceImpl<PrescriptionReviewSettingsMapper, PrescriptionReviewSettings> implements PrescriptionReviewSettingsService {

    @Resource
    private PrescriptionReviewSettingsMapper prescriptionReviewSettingsMapper;

    @Override
    public PrescriptionReviewSettings getPrescriptionReviewSettings() {
        String merchantId = WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID);
        List<PrescriptionReviewSettings> list = prescriptionReviewSettingsMapper.selectList(null);
        System.out.println("我在查询设置 所有的......");
        System.out.println(list.size());
        PrescriptionReviewSettings prescriptionReviewSettings =list.get(0);
        if(ObjectUtil.isEmpty(prescriptionReviewSettings)){
            //如果为空则新增一条
            prescriptionReviewSettings = new PrescriptionReviewSettings();
            prescriptionReviewSettings.setInternetPrescription(1);
            prescriptionReviewSettings.setPharmacistReview(0);
            prescriptionReviewSettings.setMerchantId(Long.valueOf(merchantId));
            prescriptionReviewSettings.setCreateTime(new Date());
            prescriptionReviewSettings.setUpdateTime(new Date());
            prescriptionReviewSettingsMapper.insert(prescriptionReviewSettings);
            return prescriptionReviewSettings;

        }else {
            return prescriptionReviewSettings;
        }
    }

    @Override
    public Boolean editPrescriptionReviewSettings(EditPrescriptionReviewSettingsReq req) {
        PrescriptionReviewSettings prescriptionReviewSettings =
               prescriptionReviewSettingsMapper.selectList(null).get(0);
        if(ObjectUtil.isEmpty(prescriptionReviewSettings)){
            //如果为空则新增一条
            prescriptionReviewSettings = new PrescriptionReviewSettings();
            prescriptionReviewSettings.setInternetPrescription(req.getInternetPrescription());
            prescriptionReviewSettings.setPharmacistReview(req.getPharmacistReview());
            prescriptionReviewSettings.setCreateTime(new Date());
            prescriptionReviewSettings.setUpdateTime(new Date());
            prescriptionReviewSettingsMapper.insert(prescriptionReviewSettings);
        }else {
            prescriptionReviewSettings.setInternetPrescription(req.getInternetPrescription());
            prescriptionReviewSettings.setPharmacistReview(req.getPharmacistReview());
            prescriptionReviewSettings.setCreateTime(new Date());
            prescriptionReviewSettings.setUpdateTime(new Date());
            prescriptionReviewSettingsMapper.updateById(prescriptionReviewSettings);
        }
        return true;
    }

    @Override
    public PrescriptionReviewSettings appletPrescriptionReviewSettings() {
        String merchantId = WebUtil.getRequest().getHeader(AuthConstant.MERCHANT_ID);

        PrescriptionReviewSettings prescriptionReviewSettings =
                prescriptionReviewSettingsMapper.selectList(null).get(0);
        if(ObjectUtil.isEmpty(prescriptionReviewSettings)){
            //如果为空则新增一条
            prescriptionReviewSettings = new PrescriptionReviewSettings();
            prescriptionReviewSettings.setInternetPrescription(1);
            prescriptionReviewSettings.setPharmacistReview(0);
            prescriptionReviewSettings.setMerchantId(Long.valueOf(merchantId));
            prescriptionReviewSettings.setCreateTime(new Date());
            prescriptionReviewSettings.setUpdateTime(new Date());
            prescriptionReviewSettingsMapper.insert(prescriptionReviewSettings);
            return prescriptionReviewSettings;

        }else {
            return prescriptionReviewSettings;
        }    }
}

